// pages/shopStore/detail/index.js
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        parameter: {
            'navbar': '1',
            'return': '1',
            'title': '店铺详情',
            'color': true,
            'class': false
        },
        pageHeight: app.globalData.pageHeight, //手机屏幕高度
        navHeight: app.globalData.navHeight,
        imagesURL: '',
        shopInfo: {},
        stockList: [],
        goodsName: '',
        dealerTabbarType: '1',
        wxLiteLoginParam:{
            jsCod:''
        },
        storeForm: {
            image: '',
            shopType:'',
            clerksNumber:0,
            dealerId:'',
            natives:'',
            sex:'',
            shopProperties:'',
            dealer_region_id:null,
            dealer_region_province_id:null,
            dealer_region_city_id:null,
            userName: '',
            age:''
        }
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.setData({
            shopInfo: JSON.parse(options.detail),
            imagesURL: app.imagesURL,
            'storeForm.dealerId': JSON.parse(options.detail).dealerId
        })
        this.getStock(this.data.shopInfo.dealerId)
    },


    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
    
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },
    /* 搜搜商品 */
    searchShop(e) {
        this.setData({
            goodsName: e.detail.value
        })
        this.getStock(this.data.shopInfo.dealerId)
    },
    bindKeyInput1: function (e) {
        this.setData({
            'storeForm.clerksNumber': e.detail.value
        })
        this.getSaveDealer()
    },
    bindKeyInput2: function (e) {
        this.setData({
            'storeForm.shopProperties': e.detail.value
        })
        this.getSaveDealer()
    },
    bindKeyInput3: function (e) {
        console.log(e.detail.value)
    },
    bindKeyInput4: function (e) {
        this.setData({
            'storeForm.userName': e.detail.value
        })
        this.getSaveDealer()
    },
    bindKeyInput5: function (e) {
        console.log(e.detail.value)
    },
    toggleDealerType(e){
        this.setData({
            'storeForm.shopType': e.currentTarget.dataset.index
        })
        this.getSaveDealer()
        
    },
    bindRegionChange(e){
        this.setData({
            'storeForm.natives': e.detail.value.join('')
        })
        this.getSaveDealer()
    },
    toggleShopkeeperSex(e){
        this.setData({
            'storeForm.sex': e.currentTarget.dataset.index
        })
        this.getSaveDealer()
    },
    toggleShopkeeperAge(e){
        this.setData({
            'storeForm.age': e.currentTarget.dataset.index
        })
        this.getSaveDealer()
    },
    // 查询店铺详情/insertDealerInfo/findDealerInfos/
    getDealerDetail(){
        app.baseGet(app.U({
            m: 'bdservice',
            c: 'insertDealerInfo',
            a: 'findDealerInfos/',
            q: {
                dealerId: this.data.shopInfo.dealerId //店铺id
            }
        }), res => {}, (res) => {
            this.setData({
                storeForm:res.data
            })
        })
    },
    // 保存信息
    getSaveDealer(){
        app.basePost(app.U({
            m: 'bdservice',
            c: 'insertDealerInfo',
            a: 'updateDealerInfo/'
        }), this.data.storeForm, res => {
            console.log(res)
        }, (res) => {})
    },
    // 选择图片 &&&
    uploadStoreImg: function (e) {
        var that = this;
        var imgbox = this.data.imgbox;
        wx.chooseImage({
            count: 1, // 默认9，设置图片张数
            sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
            sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
            success: function (res) {
                var tempFilePaths = res.tempFilePaths,
                    fileIDs = that.data.fileIDs ? that.data.fileIDs : [],
                    image = tempFilePaths
                that.setData({
                    imgbox: image
                });
                var url = app.U({
                    m: 'api',
                    c: 'task',
                    a: 'worker/distribution/upload',
                })
                wx.showLoading({
                    title: '上传中',
                })
                wx.uploadFile({
                    url: url,
                    filePath: that.data.imgbox[that.data.imgbox.length - 1],
                    name: 'file',
                    formData: {
                        'filename': 'pics',
                    },
                    header: {
                        "Content-Type": "multipart/form-data",
                        authToken: wx.getStorageSync('token') ? wx.getStorageSync('token') : '',
                        requestTerminal: 20
                    },
                    success: function (res) {
                        if (res.statusCode == 200) {
                            that.setData({
                                'storeForm.image': res.data
                            })
                            that.getSaveDealer()
                            wx.hideLoading();
                            wx.showToast({
                                title: "上传成功",
                            })
                        } else {
                            wx.hideLoading();
                            wx.showToast({
                                title: "上传失败,请重新上传",
                            })
                            that.signIn()
                        }
                    },
                    fail: function (res) {
                        wx.hideLoading();
                        wx.showToast({
                            title: "上传失败",
                        })
                        
                    }
                })
            }
        })
    },
    // 获取UNinID需要的code
    getCode() {
        let that = this;
        wx.login({
            success(res) {
                that.setData({
                    'wxLiteLoginParam.jsCode': res.code
                })
            }
        })
    },
    // 上传失败刷新code
    signIn() {
        this.getCode()
        var userpass = wx.getStorageSync('userpass')
        var data = {
            password: userpass.password,
            username: userpass.username,
            wxLiteSettingsId: 4,
            jsCode: this.data.wxLiteLoginParam.jsCode
        }
        app.basePost(app.U({
            m: 'factory',
            c: 'sysuser',
            a: 'login'
        }), data, res => {
            if (res.code == 200) {
                wx.setStorage({
                    key: 'token', //key名
                    data: res.data.token //值
                })
                wx.setStorage({
                    key: 'userpass', //key名
                    data: data //值
                })
            }
        });
    },
    /* 
      查询店铺库存
    */
    getStock(dealerId) {
        var that = this;
        app.baseGet(app.U({
            m: 'bdservice',
            c: 'service',
            a: 'findDealerInfo/',
            q: {
                dealerId: dealerId, //店铺id
                goodsName: that.data.goodsName
            }
        }), res => {}, (res) => {
            that.setData({
                stockList: res.data
            })
        })
    },
    //  切换
    toggleDealerTabbar(e) {
        this.setData({
            dealerTabbarType: e.currentTarget.dataset.index
        })
        if(e.currentTarget.dataset.index == 2){
            this.getDealerDetail()
        }
        
    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})